package com.front.bean;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import com.front.cache.manager.FrontRedisManager;
import com.front.common.StringUtils;
import com.front.constant.FrontConstant;
import com.front.constant.ReportConstant;
import com.front.dao.CmsJdbcBaseDao;

public class InitSystemCacheBean implements ApplicationListener<ContextRefreshedEvent>{

	@Autowired
	private CmsJdbcBaseDao jdbcBaseDao;
	
	@Autowired
	private FrontRedisManager redisManager;
	
	private String mode;
	 
	public void init()  {
		if(StringUtils.equals(mode, FrontConstant.PLATFORM_CACHE_PRODUCT))
		{
			makeRepCache();
		}
	}
	
	
	public void setMode(String mode) {
		this.mode = mode;
	}
	
	/***
	 *  构建repCode的cache
	 * @throws Exception
	 */
	private void makeRepCache() 
	{
		try
		{
		String reportCode = null;
		List<Map<String,Object>>reportList =  jdbcBaseDao.getResultBySql(ReportConstant.REPORT_CACHE_SQL, null);
		if(reportList!=null&&!reportList.isEmpty())
		{
			 for(Map<String,Object>report:reportList)
			 {
				 reportCode =StringUtils.obj2String(report.get("REP_CODE"));
				 redisManager.set(reportCode, report);
			 }
		}
		}catch (Exception e) {
			 e.printStackTrace();
		}
	}


	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		  ApplicationContext ac = event.getApplicationContext();
	      if(ac!=null&&ac.getParent()==null)
	      {
	         init();
	      }
	}


 

}
